package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.ISysDeptVideoMapper;
import com.wejoy.video.middle.station.data.domain.system.SysDeptVideo;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysDeptVideoService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 部门视频资源表
 *
 * @author miracle_j
 * @date 2024年03月25日 10:32:34
 */
@Service
public class SysDeptVideoServiceImpl implements ISysDeptVideoService {
    @Autowired
    private ISysDeptVideoMapper sysDeptVideoMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(SysDeptVideo req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysDeptVideo req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysDeptVideoMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysDeptVideo sysDeptVideo) {
        WJAssert.notNull(sysDeptVideo, "入参对象不能为空");
        WJAssert.notNull(sysDeptVideo.getId(), "id参数错误");
        SysDeptVideo where = new SysDeptVideo();
        where.setId(sysDeptVideo.getId());
        return sysDeptVideoMapper.updateSelective(sysDeptVideo, where);
    }

    @Override
    public int getCount(SysDeptVideo sysDeptVideo) {
        checkStringLength(sysDeptVideo, true);
        return sysDeptVideoMapper.getCount(sysDeptVideo);
    }

    @Override
    public SysDeptVideo getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysDeptVideo where = new SysDeptVideo();
        where.setId(id);
        return sysDeptVideoMapper.getOne(where);
    }

    @Override
    public List<SysDeptVideo> getList(SysDeptVideo sysDeptVideo) {
        checkStringLength(sysDeptVideo, true);
        return sysDeptVideoMapper.getList(sysDeptVideo);
    }

    @Override
    public List<SysDeptVideo> getPageList(SysDeptVideo sysDeptVideo) {
        checkStringLength(sysDeptVideo, true);
        return sysDeptVideoMapper.getPageList(sysDeptVideo);
    }

    @Override
    public void audit(Long id, String operator) {
        SysDeptVideo req = new SysDeptVideo();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysDeptVideo req = new SysDeptVideo();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysDeptVideo req = new SysDeptVideo();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysDeptVideo sysDeptVideo, boolean nullAble) {
        WJAssert.notNull(sysDeptVideo, "入参对象不能为空");
        WJAssert.limitMaxLength(sysDeptVideo.getVideoUrl(), true, 255, "视频资源地址不合法!");
        WJAssert.limitMaxLength(sysDeptVideo.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(sysDeptVideo.getRemark(), true, 255, "备注不合法!");
    }
}
